// admin/system/handler/sys_job_log_handler.go
package handler

import (
	"github.com/gin-gonic/gin"
	"go-admin1/admin/system/model"
	"go-admin1/admin/system/service"
	"go-admin1/internal/common/utils"

	"strconv"
)

// SysJobLogHandler 任务日志处理器
type SysJobLogHandler struct {
	jobLogService service.SysJobLogService
}

// NewSysJobLogHandler 创建任务日志处理器实例
func NewSysJobLogHandler(jobLogService service.SysJobLogService) *SysJobLogHandler {
	return &SysJobLogHandler{
		jobLogService: jobLogService,
	}
}

// Create 创建任务日志
func (h *SysJobLogHandler) Create(c *gin.Context) {
	var jobLog model.SysJobLog
	if err := c.ShouldBindJSON(&jobLog); err != nil {
		//	response.Error(c, http.StatusBadRequest, "参数解析失败: "+err.Error())
		return
	}

	if err := h.jobLogService.Create(&jobLog); err != nil {
		//		response.Error(c, http.StatusInternalServerError, "创建任务日志失败: "+err.Error())
		return
	}

	//		response.Success(c, "创建任务日志成功", jobLog)
}

// GetByID 根据ID获取任务日志
func (h *SysJobLogHandler) GetByID(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		//		response.Error(c, http.StatusBadRequest, "参数解析失败: "+err.Error())
		return
	}

	jobLog, err := h.jobLogService.GetByID(id)
	if err != nil {
		//		response.Error(c, http.StatusInternalServerError, "获取任务日志失败: "+err.Error())
		return
	}
	utils.OkResponse(c, jobLog)
	//		response.Success(c, "获取任务日志成功", jobLog)
}

// Delete 删除任务日志
func (h *SysJobLogHandler) Delete(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		//			response.Error(c, http.StatusBadRequest, "参数解析失败: "+err.Error())
		return
	}

	if err := h.jobLogService.Delete(id); err != nil {
		//			response.Error(c, http.StatusInternalServerError, "删除任务日志失败: "+err.Error())
		return
	}

	//		response.Success(c, "删除任务日志成功", nil)
}

// List 任务日志列表
func (h *SysJobLogHandler) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	jobLogs, total, err := h.jobLogService.List(page, pageSize)
	if err != nil {
		utils.FailResponse[string](c, "获取任务日志列表失败")
		return
	}
	resultData := map[string]interface{}{
		"list":  jobLogs,
		"total": total,
	}

	utils.OkResponse(c, resultData)
}

// CleanAll 清空任务日志
func (h *SysJobLogHandler) CleanAll(c *gin.Context) {
	if err := h.jobLogService.CleanAll(); err != nil {
		utils.FailResponse[string](c, "清空任务日志失败")
		return
	}
	utils.OkResponseNoDataWithMessage[string](c, "清空任务日志成功")
}
